package com.meizhao.api.utils;

import com.meizhao.api.entity.dto.request.ReqDto;
import com.meizhao.api.exception.CommonException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Collection;
import java.util.List;


@Slf4j
public final class CheckUtils {


    public static Object dtoNotNull(ReqDto dto, String msg){
        if (dto == null) {
            log.info(msg);
            throw new CommonException(ResultCode.ERROR, msg);
        }
        Object obj = dto.getBody();
        if (obj == null) {
            log.info(msg);
            throw new CommonException(ResultCode.ERROR, msg);
        }
        return obj;
    }

    public static void notNull(String msg, Object... objs){
        if (objs == null) {
            log.info(msg);
            throw new CommonException(ResultCode.ERROR, msg);
        }
        for(Object object : objs){
            notNull(object, msg);
        }
    }


    public static void notEqual(String value1, String value2, String msg){

        if(!(value1 != null && value1.equals(value2))){
            throw new CommonException(ResultCode.ERROR, msg);
        }
    }

    public static void notNull(Object object, String msg) {
        if (object == null) {
            log.info(msg);
            throw new CommonException(ResultCode.ERROR, msg);
        }
        if(object instanceof String){
           if(StringUtils.isBlank((String)object)) {
               log.info(msg);
               throw new CommonException(ResultCode.ERROR, msg);
           }
        }

    }


    public static void maxLength(Integer length, String str, String msg){

        if(StringUtils.trim(str).length() > length){
            throw new CommonException(ResultCode.ERROR, msg+"不能超过"+length+"个字符");
        }
    }

    public static void exist(Object object, String msg) {
        if (object != null) {
            log.info(msg);
            throw new CommonException(ResultCode.ERROR, msg);
        }
    }

    public static void notExist(Object object, String msg) {
        if (object == null) {
            log.info(msg);
            throw new CommonException(ResultCode.ERROR, msg);
        }
    }

    public static void notBlank(String str, String msg) {
        if (StringUtils.isBlank(str)) {
            log.info(msg);
            throw new CommonException(ResultCode.ERROR, msg);
        }
    }

    public static void notEmpty(Collection collection, String msg) {
        if(!(collection != null && collection.size() > 0)){
            log.info(msg);
            throw new CommonException(ResultCode.ERROR, msg);
        }
    }

    public static void checkFile(MultipartFile file, Long fileSizeLimit, List<String> fileTypes){

        if(file == null){
            log.info("不能上传空文件");
            throw new CommonException(ResultCode.ERROR, "不能上传空文件");
        }
        if(file.getSize() == 0){
            log.info("不能上传空文件");
            throw new CommonException(ResultCode.ERROR, "不能上传空文件");
        }
        if(file.getSize() > fileSizeLimit){
            log.info("上传文件不能大于"+(fileSizeLimit/1024/1024)+"MB");
            throw new CommonException(ResultCode.ERROR, "上传文件不能大于"+(fileSizeLimit/1024/1024)+"MB");
        }
        try {
            if (OtherUtils.getFileType(fileTypes, IOUtils.toByteArray(file.getInputStream())) == null) {
                String[] types = fileTypes.toArray(new String[fileTypes.size()]);
                String[] strs = new String[fileTypes.size()];
                for(int i=0,j=types.length;i<j;i++){
                    strs[i] = types[i].split("_")[0].substring(1);
                }
                String tips = StringUtils.join(strs,",");
                log.info("上传文件类型只能是:{}", tips);
                throw new CommonException(ResultCode.ERROR, "上传文件只能是: " + tips);
            }
        }catch (IOException e){
            log.info("无法获取上传文件类型");
            throw new CommonException(ResultCode.ERROR, "无法获取上传文件类型");

        }
    }


}
